var pay = new Vue({
    el: "#app",
    data: {
        store_id: '',//获取店铺id
        isActive: false,//打开购物券抽屉
        payMoney: '',//input输入的金额
        payList: '',//所有的购物券
        payList2: [],//符合条件的购物券
        payList3: [],//不符合条件的购物券
        selectIndex: 0,//选择第几个购物券
        choose: false,//是否有购物券被选中
        deductions: '',//抵扣的金额
        cashcardId: '',//代金券id
        client_type: ''
    },
    //计算属性
    computed: {
        cashcardNum() {
            // let payList2 = [];
            let j = 0;
            let l = 0;
            this.payList2 = [];
            this.payList3 = [];
            if (this.payList.length != 0) {
                for (let i = 0; i < this.payList.length; i++) {
                    if (this.payMoney >= this.payList[i].min_order_price) {
                        this.payList2[j] = this.payList[i];
                        j++
                    } else {

                        this.payList3[l] = this.payList[i];
                        l++
                    }
                }
            } else {
                return 0
            }
            return j;
        },
        actual_pay() {
            return (this.payMoney - this.deductions);
        }

    },
    //监听属性
    watch: {},
    //钩子函数
    created() {
        this.store_id = this.getUriParam('m');
        this.client_type = client_type;
    },
    mounted() {
        this.getCheck()
    },
    methods: {
        getCheck() {
            var that = this
            s5.initData({
                url: '/cashcard-check',
                type: 'post',
                data: {
                    store_id: that.store_id,
                }
            }, (res) => {
                var data = JSON.parse(res)
                that.payList = data.data.list;
                if (data.code != 200) {
                    weui.confirm(data.msg, {
                        title: '温馨提示',
                        buttons: [{
                            label: 'NO',
                            type: 'default',
                            onClick: function () {
                                console.log('no')
                            }
                        }, {
                            label: 'YES',
                            type: 'primary',
                            onClick: function () {
                                window.location.href = 'index.html'
                                console.log('yes')
                            }
                        }]
                    });
                }
            });
        },
        open(num) {
            var that = this
            that.isActive = true;
            that.deductions = num;
            that.cashcardId = this.payList2[0].id;
        },
        check(index, money, id) {
            var that = this
            that.deductions = money
            if (that.selectIndex == index) {
                that.selectIndex = -1;
                that.choose = false;
                that.cashcardId = '';
            } else {
                that.selectIndex = index
                that.choose = true
                that.cashcardId = id;
            }
        },
        pay() {
            var that = this;
            // alert(that.store_id);
            // alert(that.payMoney);
            // alert(that.cashcardId);
            s5.initData({
                url: '/store-pay',
                type: 'post',
                data: {
                    store_id: that.store_id,
                    price: that.payMoney,
                    card_id: that.cashcardId,
                }
            }, (res) => {
                var data = JSON.parse(res)
                // return ;
                if (data.code != 200) {//报错
                    weui.confirm('温馨提示：' + data.msg, {
                        title: '温馨提示',
                        buttons: [{
                            label: '继续支付',
                            type: 'default',
                            onClick: function () {
                                console.log('no')
                            }
                        }, {
                            label: '返回首页',
                            type: 'primary',
                            onClick: function () {
                                window.location.href = 'index.html'
                                console.log('yes')
                            }
                        }]
                    });
                } else {//继续调起支付
                    // console.log(data.data);return;
                    if (that.client_type === 'wechat') {//调起微信支付
                        let pay_data = data.data
                        let wepay_config = {
                            appId: pay_data.AppId,
                            timeStamp: pay_data.TimeStamp,
                            package: pay_data.Subject,
                            nonceStr: pay_data.Charset,
                            signType: pay_data.SignType,
                            paySign: pay_data.Sign,
                        };

                        WeixinJSBridge.invoke(
                            'getBrandWCPayRequest', wepay_config,
                            function (result) {
                                if (result.err_msg == "get_brand_wcpay_request:ok") {
                                    weui.topTips('支付成功', {className: 'success'});
                                } else {
                                    weui.topTips('支付失败');
                                }
                            });
                    } else if (that.client_type === 'alipay') {//调起支付宝支付
                        // 由于js的载入是异步的，所以可以通过该方法，当AlipayJSBridgeReady事件发生后，再执行callback方法
                        function ready(callback) {
                            if (window.AlipayJSBridge) {
                                callback && callback();
                            } else {
                                document.addEventListener('AlipayJSBridgeReady', callback, false);
                            }
                        }

                        function tradePay(tradeNO) {
                            ready(function () {
                                // 通过传入交易号唤起快捷调用方式(注意tradeNO大小写严格)
                                AlipayJSBridge.call("tradePay", {
                                    tradeNO: tradeNO
                                }, function (data) {
                                    if ("9000" == data.resultCode) {
                                        weui.topTips('支付成功', {className: 'success'});
                                    } else {
                                        weui.topTips('支付失败');
                                    }
                                });
                            });
                        }

                        tradePay(data.data.TradeNo);
                    } else {
                        weui.topTips('[请使用微信或支付宝访问]');
                    }
                }
            })
        },

        close() {
            this.isActive = false;
            if (this.selectIndex == 0) {
                this.choose = true
            }
        },

        getUriParam(variable) {
            let query = window.location.search.substring(1);
            let vars = query.split("&");
            for (let i = 0; i < vars.length; i++) {
                let pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
            return (false);
        }
    }
})
// :class="{isCheack:selectIndex==index}"